import java.util.HashMap;

/**
 * @version Create on 2012-11-13
 * @author Yinzi Chen
 */

public class RomanToInteger {

	static final char roman[] = new char[] { 'I', 'V', 'X', 'L', 'C', 'D', 'M' };
	static final int cnt[] = new int[] { 1, 5, 10, 50, 100, 500, 1000 };
	static HashMap<Character, Integer> map;

	static {
		map = new HashMap<Character, Integer>();
		for (int i = 0; i < roman.length; ++i)
			map.put(roman[i], cnt[i]);
	}

	public int romanToInt(String s) {
		int res = 0;
		int val = map.get(s.charAt(0));
		res += val;
		int pre = val;
		for (int i = 1; i < s.length(); ++i) {
			val = map.get(s.charAt(i));
			res += val;
			if (val > pre) {
				res -= pre + pre;
			}
			pre = val;
		}
		return res;
	}

	public static void main(String[] args) {

	}

}
